package com.smartleanx.module.hrm.service.employee.listenser;

import com.smartleanx.module.bpm.enums.task.BpmProcessInstanceStatusEnum;
import com.smartleanx.module.bpm.event.BpmProcessInstanceStatusEvent;
import com.smartleanx.module.bpm.event.BpmProcessInstanceStatusEventListener;
import com.smartleanx.module.hrm.eums.employee.ProcessDefinitionEnum;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 员工离职流程状态监听器
 */
@Slf4j
@Component
public class BpmEmployeeLeaveListener extends BpmProcessInstanceStatusEventListener {

    @Resource
    private EmployeeService employeeService;


    @Override
    protected String getProcessDefinitionKey() {
        return ProcessDefinitionEnum.SEPARATION.getKey();
    }

    @Override
    protected void onEvent(BpmProcessInstanceStatusEvent event) {
        if (BpmProcessInstanceStatusEnum.APPROVE.getStatus().equals(event.getStatus())) {
            System.out.println("event.getStatus() = " + event.getStatus());
            employeeService.approvedLeave(Long.valueOf(event.getBusinessKey()), event.getStatus());
        } else if (BpmProcessInstanceStatusEnum.REJECT.getStatus().equals(event.getStatus())
                || BpmProcessInstanceStatusEnum.CANCEL.getStatus().equals(event.getStatus())) {
            System.out.println("event.getStatus() = " + event.getStatus());
            employeeService.rejectLeave(Long.valueOf(event.getBusinessKey()), event.getStatus());
        }
    }
}
